import Vue from 'vue'
import VueRouter from 'vue-router'
import Cookies from 'js-cookie'
import NotFound from '../views/NotFound'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import Video from '../views/Video.vue'
import Search from '../views/Search'
import Update from '../views/Update'

import Mine from '../views/Mine/Mine.vue'
import Setting from '../views/Mine/content/Setting.vue'
import Upload from '../views/Mine/content/Upload.vue'
import MyVideo from '../views/Mine/content/Video.vue'
import Favorites from '../views/Mine/content/Favorites'
import Message from '../views/Mine/content/message/Message'
import SystemMsg from '../views/Mine/content/message/SystemMsg'
import UserMsg from '../views/Mine/content/message/UserMsg'
import Following from '../views/Mine/content/Following'
import Follower from '../views/Mine/content/Follower'

import UserSpace from '../views/OtherUser/UserSpace'
import UserVideo from '../views/OtherUser/content/Video'
import UserFollowing from '../views/OtherUser/content/Following'
import UserFollower from '../views/OtherUser/content/Follower'

import AdminLogin from '../views/Admin/Login'
import Admin from '../views/Admin/Home'
import User from '../views/Admin/User/User'
import Announce from '../views/Admin/User/Announce'
import AdminVideo from '../views/Admin/User/Video'
import Carousel from '../views/Admin/User/Carousel'

//解决elementUI导航栏重复点击报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: Home
  },
  {
    path:'/login',
    name: 'login',
    component: Login
  },
  {
    path:'/register',
    name: 'register',
    component: Register
  },
  {
    path:'/search/:search',
    name: 'search',
    component: Search
  },
  {
    path:'/video/VID:vid',
    name:'video',
    component:Video
  },
  {
    path: '/admin/login',
    name: 'adminLogin',
    component: AdminLogin,
  },
  {
    path:'/admin',
    name:'admin',
    component:Admin,
    redirect: '/admin/user',
    children:[
      {
        path: '/admin/user',
        name: 'user',
        component: User,
      },
      {
        path: '/admin/announce',
        name: 'announce',
        component: Announce,
      },
      {
        path: '/admin/video',
        name: 'adminVideo',
        component: AdminVideo,
      },
      {
        path: '/admin/carousel',
        name: 'carousel',
        component: Carousel,
      },
    ]
  },
  {
    path:'/mine',
    name:'mine',
    redirect: '/mine/video',
    meta:{
      auth:true
    },
    component:Mine,
    children: [
      {
        path:'/mine/setting',
        name:'setting',
        meta:{
          auth:true
        },
        component:Setting
      },
      {
        path:'/mine/upload',
        name:'upload',
        meta:{
          auth:true
        },
        component:Upload
      },
      {
        path:'/mine/video',
        name:'myVideo',
        meta:{
          auth:true
        },
        component:MyVideo
      },
      {
        path:'/mine/favorites',
        name:'favorites',
        meta:{
          auth:true
        },
        component:Favorites
      },
      {
        path:'/mine/message',
        name:'message',
        meta:{
          auth:true
        },
        component:Message,
        children:[
          {
            path:'/mine/message/system',
            name:'systemMsg',
            meta:{
              auth:true
            },
            component:SystemMsg,
          },
          {
            path:'/mine/message/user',
            name:'userMsg',
            meta:{
              auth:true
            },
            component:UserMsg,
          }
        ]
      },
      {
        path:'/mine/following',
        name:'following',
        meta:{
          auth:true
        },
        component:Following
      },
      {
        path:'/mine/follower',
        name:'follower',
        meta:{
          auth:true
        },
        component:Follower
      }
    ]
  },
  {
    path:'/userSpace',
    name:'userSpace',
    component:UserSpace,
    redirect: '/userSpace/video/:uid',
    children:[
      {
        path:'/userSpace/video/:uid',
        name:'userVideo',
        component:UserVideo
      },
      {
        path:'/userSpace/following/:uid',
        name:'userFollowing',
        component:UserFollowing
      },
      {
        path:'/userSpace/follower/:uid',
        name:'userFollower',
        component:UserFollower
      },
    ]
  },
  {
    path:'/update',
    name:'update',
    component: Update,
  },
  {
    path:'/404',
    name:'404',
    component: NotFound,
  },
  {
     path:'*',
     redirect:{
         name:"404"
     }
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next) => {
  //是否需要登录
  if(to.meta.auth){
    //是否存在token
    if(Cookies.get('token')){   
      next()
    }else{
      router.push({name:'login'})
    }
  }else{
    next()
  }
})

export default router
